﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GolfLeague.EditModels;
using GolfLeague.Models;
using GolfLeague.ViewModels;

namespace GolfLeague.Controllers
{
    public class ScoreController : Controller
    {
        HandicapInterface handicapInterface = new HandicapInterface();
        MatchInterface matchInterface = new MatchInterface();
        RoundInterface roundInterface = new RoundInterface();
        PlayerInterface playerInterface = new PlayerInterface();
        ScheduleInterface scheduleInterface = new ScheduleInterface();
        ScoreCardInterface scoreCardInterface = new ScoreCardInterface();
        ScoreInterface scoreInterface = new ScoreInterface();

        //
        // GET: /Score/

        [Authorize(Roles = "Administrator")]
        public ActionResult Index(int? id)
        {
            return View(scoreInterface.GetScoresEditModel(id));
        }

        //
        // GET: /Score/Matches/5

        [Authorize(Roles = "Administrator")]
        public PartialViewResult Matches(int roundID)
        {
            PartialViewResult matchView = new PartialViewResult();
            RoundViewModel round = roundInterface.GetRoundViewModel(roundID);
            matchView.ViewData.Model = round;
            matchView.ViewName = "_Matches";

            return matchView;
        }

        //
        // POST: /Score/CalculatePoints/5
        [HttpPost]
        [Authorize(Roles = "Administrator")]
        public JsonResult CalculatePoints(int id)
        {
            bool PointsCalculated = scoreInterface.CalculateRoundPoints(id);

            return Json(new { PointsCalculated });
        }

        //
        // GET: /Score/Scorecard/5

        [Authorize(Roles = "Administrator")]
        public PartialViewResult Scorecard(int id)
        {

            PartialViewResult partialView = new PartialViewResult();
            partialView.ViewData.Model = scoreInterface.GetScoresScorecardEditModel(id);
            partialView.ViewName = "_Scorecard";
            return partialView;
        }

        //
        // POST:/Score/Scorecard/
        [HttpPost]
        [Authorize(Roles = "Administrator")]
        public JsonResult Scorecard(ScoresScorecardEditModel scoresScorecardEditModel)
        {
            bool ScoreUpdates = false;

            if (ModelState.IsValid)
                ScoreUpdates = scoreInterface.SaveMatchScores(scoresScorecardEditModel);

            return Json(new { ScoreUpdates });
        }

        //
        // GET: /Score/RoundDropdown/
        [Authorize(Roles = "Administrator")]
        public JsonResult RoundDropdown(int scheduleID)
        {
            List<RoundsDropDownList> roundsDropDownList = new List<RoundsDropDownList>();
            foreach (Round round in roundInterface.GetAllScheduleRounds(scheduleID))
            {
                roundsDropDownList.Add(new RoundsDropDownList
                {
                    RoundID = round.RoundID,
                    RoundDate = round.RoundDate.ToShortDateString()
                });
            }
            if (roundsDropDownList.Count == 0) roundsDropDownList.Add(new RoundsDropDownList { RoundID = 0, RoundDate = "No rounds exist for this schedule." });
            return Json(new { roundsDropDownList }, JsonRequestBehavior.AllowGet);
        }

        //
        // GET:/Score/PlayerHandicap/
        [Authorize(Roles = "Administrator")]
        public JsonResult PlayerHandicap(string playerID, string matchID)
        {
            int playerHandicap = playerInterface.GetPlayersCurrentHandicap(Convert.ToInt32(playerID)).Handicap;
            Match match = matchInterface.GetMatch(Convert.ToInt32(matchID));
            CourseSides courseSide = (CourseSides)match.Round.SideID;
            if (courseSide != CourseSides.Both)
            {
                int frontPar = scoreCardInterface.GetFrontHoleDetails(match.Round.TeeBoxID).Sum(h => h.Par);
                int backPar = scoreCardInterface.GetBackHoleDetails(match.Round.TeeBoxID).Sum(h => h.Par);
                playerHandicap = scoreCardInterface.GetPlayerHandicap(playerHandicap, frontPar, backPar, courseSide);
            }
            return Json(new { playerHandicap }, JsonRequestBehavior.AllowGet);
        }
    }
}
